package com.lanou.moving.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lanou.moving.R;
import com.lanou.moving.activity.MVPlayActivity;
import com.lanou.moving.adapter.MVListHotAdapter;
import com.lanou.moving.base.BaseFragment;
import com.lanou.moving.entity.MVHotRoot;
import com.lanou.moving.utils.SwipeRefreshLoadingLayout;
import com.lanou.moving.volley.StringRequestUtf;
import com.lanou.moving.volley.VolleySingleton;

import java.util.List;


/**
 * Created by 梁树珍 on 15/11/4.
 */
public class MVListHotFragment extends BaseFragment implements AdapterView.OnItemClickListener, SwipeRefreshLoadingLayout.OnRefreshListener, SwipeRefreshLoadingLayout.OnLoadListener {
    private VolleySingleton volleySingleton;
    private int page = 1;
    private int order ;
    private int typeId ;
    private List<MVHotRoot.DataEntity> dataEntities;
    private MVHotRoot.DataEntity dataEntity;
    private MVListHotAdapter adapter;
    private String url_start;
    private String url_end;
    private String url_mid;
    private SwipeRefreshLoadingLayout mv_list_recent_loading_layout;
    private GridView mv_hot_gridView;

    public static BaseFragment newInstance(String url_start, String url_end, String url_mid, int order, int typeId) {
        BaseFragment f = new MVListHotFragment();
        Bundle args = new Bundle();
        args.putString("url_end", url_end);
        args.putString("url_start", url_start);
        args.putString("url_mid", url_mid);
        args.putInt("order", order);
        args.putInt("typeId", typeId);
        f.setArguments(args);
        return f;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_song_list_hot, null);
        volleySingleton = VolleySingleton.getInstance(mContext);
        mv_hot_gridView = (GridView) view.findViewById(R.id.song_list_hot_gv);
        mv_hot_gridView.setOnItemClickListener(this);
        mv_hot_gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        mv_list_recent_loading_layout = (SwipeRefreshLoadingLayout) view.findViewById(R.id.song_list_hot_loading_layout);
        init();
        initData();
        return view;
    }

    private void init() {
        // 取消点击效果
        mv_list_recent_loading_layout.setOnRefreshListener(this);
        mv_list_recent_loading_layout.setOnLoadListener(this);
        // 设置刷新效果为透明色
        mv_list_recent_loading_layout.setTopColor(R.color.transparent,
                R.color.transparent, R.color.transparent, R.color.transparent);
        mv_list_recent_loading_layout.setBottomColor(R.color.theme, R.color.white,
                R.color.theme, R.color.white);
    }

    private void initData() {
        url_start = getArguments().getString("url_start");
        url_mid = getArguments().getString("url_mid");
        url_end = getArguments().getString("url_end");
        typeId = getArguments().getInt("typeId");
        order = getArguments().getInt("order");
        String url = url_start + typeId + url_mid + page + url_end + order;
        StringRequestUtf stringRequestUtf = new StringRequestUtf(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        MVHotRoot hotRoot = gson.fromJson(response, MVHotRoot.class);
                        dataEntities = hotRoot.getData();
                        adapter = new MVListHotAdapter(getActivity(), dataEntities);
                        mv_hot_gridView.setAdapter(adapter);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(stringRequestUtf);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), MVPlayActivity.class);
        intent.putExtra("videoId", dataEntities.get(position).getMvList().get(0).getVideoId());
        startActivity(intent);
    }

    @Override
    public void onRefresh() {
        mv_list_recent_loading_layout.setRefreshing(false);
    }

    @Override
    public void onLoad() {
        url_start = getArguments().getString("url_start");
        url_mid = getArguments().getString("url_mid");
        url_end = getArguments().getString("url_end");
        typeId = getArguments().getInt("typeId");
        order = getArguments().getInt("order");
        page++;
        String url = url_start + typeId + url_mid + page + url_end + order;
        StringRequestUtf stringRequestUtf = new StringRequestUtf(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        mv_list_recent_loading_layout.setLoading(false);
                        Gson gson = new Gson();
                        MVHotRoot hotRoot = gson.fromJson(response, MVHotRoot.class);
                        dataEntities = hotRoot.getData();
                        adapter.addDatas(dataEntities);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(stringRequestUtf);
    }
}
